开发者

How to pass parameters / arguments using jQuery?

<script src="http://code.jquery.com/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
    function chooseLocationType(location, title, demand){
        alert(demand);
        }

<body>
<input 
    name="tempstaff_location" 
    class="ques_radio" 
    id='tempstaff_location'
    value='location_' 
    type="radio" onclick="chooseLocationType('a','b','c')" />
</body>

I have the folowing code, on click of the radio box , There is one conditional code based on this to show hide some dive.

And there is one more jQuery script which run on Click and used function.

jQuery(this)

input[name=tempstaff_location]").click(function(){
            if(jQuery(this).attr("checked")){


        });

How can I combine both开发者_如何学Python script in one.


Having your:

<script type="text/javascript">
    function chooseLocationType(location, title, demand){
        alert(demand);
        }
</script>

I would, as you don't specify where the values 'a', 'b', and 'c' come, add them as data attributes to your input

<input 
    name="tempstaff_location" 
    class="ques_radio" 
    id='tempstaff_location'
    value='location_' 
    data-location="a"
    data-title="b"
    data-demand="c"
    type="radio"/>

And finally access them in the event callback

$("input[name=tempstaff_location]").click(function(){
     if(jQuery(this).attr("checked")){
         chooseLocationType($(this).data('location'), $(this).data('title'), $(this).data('demand') );
     }
});


If the parameters vary with each element store them in a data tag within the element. Then extract them in the click handler. That way you can apply a single handler and do both. It will be easier if you change chooseLocationType to accept an array of location types.

<input 
    name="tempstaff_location" 
    class="ques_radio" 
    id='tempstaff_location'
    value='location_' 
    type="radio"
    data-location="a,b,c" />

$('input[name=tempstaff_location]").click(function(){
      if(jQuery(this).attr("checked")){
           chooseLocationType( $(this).data('location').split(/,/) );
      }
});

If the values are fixed then it's much easier, you just move the call into your handler.

<input 
    name="tempstaff_location" 
    class="ques_radio" 
    id='tempstaff_location'
    value='location_' 
    type="radio"/>

$('input[name=tempstaff_location]").click(function(){
      if(jQuery(this).attr("checked")){
           chooseLocationType( 'a','b','c' );
      }
});


When you assign an inline onclick attribute, it is just a function, basically equivalent to this code:

element.onclick = function () {
    chooseLocationType( 'a','b','c' );
};

In your JavaScript file, you can just write

document.getElementById("tempstaff_location").onclick = function () {
    if(this.checked) {
        chooseLocationType( 'a','b','c' );
    }
}

Getting an element by its id is generally better than selecting it by a tagname and attriute.


You can directly call click event on page load instead of calling in radio button html. If following is the html:

<input 
    name="tempstaff_location" 
    class="ques_radio" 
    id='tempstaff_location'
    value='location_' 
    data-location="a"
    data-title="b"
    data-demand="c"
    type="radio"/>      

Then you can call following script :

jQuery('input[name=tempstaff_location]').click(function(){
         if(jQuery(this).attr('checked')) {
           chooseLocationType(jQuery(this).attr('data-location'),jQuery(this).attr('data-title'),jQuery(this).attr('data-demand'));
         }
     });    
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜