问题
let's say if I select floating rate on the third row and I click on the equal sign button. The rest of the value of all the other row will change its value to floating rate. I want to apply tis function to all the other rows too.
Javascript:
<script type="text/javascript">
$(document).ready(function() {
$('.equal').on('click', function() {
$('.dropDown').val('Floating Rate');
})
});
</script>
Table:
<td>
<select class="dropDown" style="width:150px" name="rate_value_1">
<option value="<?php echo $_SESSION['rate_value_1'] ;?>"><?php echo $_SESSION['rate_value_1'] ;?></option>
<option value="Fixed Rate">Fixed Rate</option>
<option value="Floating Rate">Floating Rate</option>
</select>
</td>
<td><input type="button" value="select" class="equal" /></td>
</tr>
<tr>
<td>
<select class="dropDown" style="width:150px" name="rate_value_2">
<option value="<?php echo $_SESSION['rate_value_2'] ;?>"><?php echo $_SESSION['rate_value_2'] ;?></option>
<option value="Fixed Rate">Fixed Rate</option>
<option value="Floating Rate">Floating Rate</option>
</select>
</td>
<td><input type="button" value="select" class="equal" /></td>
</tr>
<tr>
<td>
<select class="dropDown" style="width:150px" name="rate_value_3">
<option value="<?php echo $_SESSION['rate_value_3'] ;?>"><?php echo $_SESSION['rate_value_3'] ;?></option>
<option value="Fixed Rate">Fixed Rate</option>
<option value="Floating Rate">Floating Rate</option>
</select>
</td>
<td><input type="button" value="select" class="equal" /></td>
</tr>
<tr>
<td>
<select class="dropDown" style="width:150px" name="rate_value_4">
<option value="<?php echo $_SESSION['rate_value_4'] ;?>"><?php echo $_SESSION['rate_value_4'] ;?></option>
<option value="Fixed Rate">Fixed Rate</option>
<option value="Floating Rate">Floating Rate</option>
</select>
</td>
<td><input type="button" value="select" class="equal" /></td>
回答1:
in jquery,
$('.dropDown').change(function(e) {
//Here you can do you programming
console.log(e.currentTarget);
});
for more info,
http://api.jquery.com/change/
回答2:
$("#dropDown").change(function () {
var str = "";
$("#dropDown option:selected").each(function () {
str += $(this).text() + " ";
});
$("div").text(str);
})
回答3:
You have a lot of problems in there. First you cannot have id="dropDown" multiple times. Use class="dropDown" instead. Second, one suggestion is to use a button instead of img.
Here is the js:
<script type="text/javascript">
$(document).ready(function() {
$('.equal').on('click', function() {
$('.dropDown').val('Floating Rate');
})
});
</script>
And html changes:
<select class="dropDown" style="width:150px" name="rate_value_1">
<input type="button" value="select" class="equal" />
回答4:
Ok Psinyee, here is what you want, and also explained.
<script type="text/javascript">
$(document).ready(function() {
$('.equal').on('click', function() {
// find coresponding dropdown
// with select inside (this is the button ->parent is the td-> previous td)
var crtDropdown = $('select', $(this).parent().prev('td'));
if(crtDropdown.prop('selectedIndex') > 0) {
// something is selected, not first option
// change values for following selects
// select ->parent td->parent tr->all next trs->select inside trs->set val to crtDropdown.val();
crtDropdown.parent().parent().nextAll('tr').find('select').val(crtDropdown.val());
}
})
});
</script>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<table>
<tr>
<td>
<select class="dropDown" style="width:150px" name="rate_value_1">
<option value="<?php echo $_SESSION['rate_value_1'] ;?>"><?php echo $_SESSION['rate_value_1'] ;?></option>
<option value="Fixed Rate">Fixed Rate</option>
<option value="Floating Rate">Floating Rate</option>
</select>
</td>
<td><input type="image" src="equal_button.png" class="equal" /></td>
</tr>
<tr>
<td>
<select class="dropDown" style="width:150px" name="rate_value_2">
<option value="<?php echo $_SESSION['rate_value_2'] ;?>"><?php echo $_SESSION['rate_value_2'] ;?></option>
<option value="Fixed Rate">Fixed Rate</option>
<option value="Floating Rate">Floating Rate</option>
</select>
</td>
<td><input type="image" src="equal_button.png" class="equal" /></td>
</tr>
<tr>
<td>
<select class="dropDown" style="width:150px" name="rate_value_3">
<option value="<?php echo $_SESSION['rate_value_3'] ;?>"><?php echo $_SESSION['rate_value_3'] ;?></option>
<option value="Fixed Rate">Fixed Rate</option>
<option value="Floating Rate">Floating Rate</option>
</select>
</td>
<td><input type="image" src="equal_button.png" class="equal" /></td>
</tr>
<tr>
<td>
<select class="dropDown" style="width:150px" name="rate_value_4">
<option value="<?php echo $_SESSION['rate_value_4'] ;?>"><?php echo $_SESSION['rate_value_4'] ;?></option>
<option value="Fixed Rate">Fixed Rate</option>
<option value="Floating Rate">Floating Rate</option>
</select>
</td>
<td><input type="image" src="equal_button.png" class="equal" /></td>
</tr>
</table>
</form>
来源:https://stackoverflow.com/questions/10975768/javascript-change-input-values-onclick-to-the-value-on-the-same-row-which-the-bu