0

Here's a code that will change a dropdown menu with the ID of "106" to a null value if I change a radio button with the ID of "101" from one option to another:

<script type="text/javascript">
jQuery(document).ready(function($){
  $('input[name="item_meta[101]"]').change(function(){
    $('select[name="item_meta[106]"]').val('');
  })
})
</script>

I need, however, to make 106 change to null only if a specific value in 101 is selected, but not just any available values. The values of the radio button id=101 are 1, 2, 3, 4, 5 respectively. So I want 106 to revert to null value if, say, 101 is changed from 5 to 4, but not from 5 to 3.

Is this possible? If so, it would obviously involved changing the ".change(function()" to some other param, but I don't know what it would be.

Thanks any advance for any help. Here's a JS Fiddle: http://jsfiddle.net/3N2TT/

UPDATE: Here's the final code that worked:

<script type="text/javascript">
jQuery(document).ready(function($){
$('input[type="radio"][name="item_meta[101]"]').change(

function () {
    var valCheck = this.value === '4 Categories - $90';
    $('select[name="item_meta[106]"]').val(function () {
        return valCheck ? 'null' : this.value;
    });
});
})
</script>

Thanks, David!

4

2 回答 2

0

Use click function and use class as the radio box selector.

jQuery(document).ready(function($){

  $('.myradiobox').click(function(){
    var radioVal = this.val();
    if(radioVal == 5 || radioVal == 4)
    {
       radioVal == 'NULL';
    }
    $('select[name="item_meta[106]"]').val(radioVal);
  })
})
于 2013-08-11T10:52:58.167 回答
0

My own take on your problem is the following:

$('input[type="radio"][name="item_meta[101]"]').change(
    function(){
        // testing if the chosen value is equal to '4' (all input values are strings):
        var valCheck = this.value === '4';

        // setting the value of the select:
        $('select[name="item_meta[106]"]').val(function(){
            /* setting to 'null' if the valCheck evaluates to true, otherwise returning
               the currently-selected value: */
            return valCheck ? 'null' : this.value;
        });
    });

JS Fiddle demo.

References:

于 2013-08-11T11:19:54.007 回答