how to define variable in jquery

后端 未结 8 1329
梦谈多话
梦谈多话 2021-01-31 09:09

I would like to know how to declare a variable in jQuery

The code I am currently using is

$.name = \'anirudha\';
alert($.name);

That co

8条回答
  •  刺人心
    刺人心 (楼主)
    2021-01-31 09:20

    jQuery is just a javascript library that makes some extra stuff available when writing javascript - so there is no reason to use jQuery for declaring variables. Use "regular" javascript:

    var name = document.myForm.txtname.value;
    alert(name);
    

    EDIT: As Canavar points out in his example, it is also possible to use jQuery to get the form value:

    var name = $('#txtname').val(); // Yes, it's called .val(), not .value()
    

    given that the text box has its id attribute set to txtname. However, you don't need to use jQuery just because you can.

提交回复
热议问题