当我单击一个选项时,如何使该选项在 JQUery 中选中?

发布于 2024-08-04 01:37:16 字数 608 浏览 3 评论 0原文

你好, 我有一个选择下拉菜单,例如

   <select id="listForms">
    <option value="Personal Form" id="25">Personal Form</option>
    <option value="Employee Details Form" id="24">Employee Details Form</option>
    <option value="Contact Form" id="45">Contact Form</option>


     </select>

当我单击一个选项时,如何使该选项在 JQUery 中被选中?

编辑:

我已经尝试过

     $("#listForms option").click(function (){
      $("#listForms option").attr("selected",true); but not working

     });

如何找到稍后选择的那个?在 JQuery 中??请建议我..

HI,
i am having a select dropdown like

   <select id="listForms">
    <option value="Personal Form" id="25">Personal Form</option>
    <option value="Employee Details Form" id="24">Employee Details Form</option>
    <option value="Contact Form" id="45">Contact Form</option>


     </select>

When i click on a option how to make that option as selected in JQUery??

Edit :

i have tried it with

     $("#listForms option").click(function (){
      $("#listForms option").attr("selected",true); but not working

     });

also how to find which one has been selected later?? in JQuery ?? please suggest me..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

冬天的雪花 2024-08-11 01:37:16
$("#listForms option:selected");

将获得选定的选项。

$("#listForms option:selected");

will get the selected option.

客…行舟 2024-08-11 01:37:16

使用以下 HTML 下面

<select id="listForms">
    <option value="Personal Form" id="25">Personal Form</option>
    <option value="Employee Details Form" id="24">Employee Details Form</option>
    <option value="Contact Form" id="45">Contact Form</option>
</select>

的 jQuery 会告诉弹出一个带有您指定的值的警报框:

$(document).ready(function() {
    $("select#listForms").change(function() {
         alert($(this).val());
    });
});

例如,当您单击“个人表单”时,您会看到一个显示“个人表单”的警报窗口。

如果您希望在选择时弹出“25”,您可以将 $(this).val() 更改为 $(this).attr('id') “个人表格”选项。

With the following HTML

<select id="listForms">
    <option value="Personal Form" id="25">Personal Form</option>
    <option value="Employee Details Form" id="24">Employee Details Form</option>
    <option value="Contact Form" id="45">Contact Form</option>
</select>

The following jQuery would tell pop up an alert box with the value you've specified:

$(document).ready(function() {
    $("select#listForms").change(function() {
         alert($(this).val());
    });
});

For example, when you click "Personal Form", you'd see an alert window that says "Personal Form".

You could change $(this).val() to $(this).attr('id') if you wanted "25" to pop up when you choose the "Personal Form" option.

眼趣 2024-08-11 01:37:16

也许这就是您所需要的:http://jgeewax.wordpress。 com/2008/09/01/jquery-dropdown-fun/

编辑 - 使用上述站点的道具:

这是他给出的最终解决方案:

<script type="text/javascript">
    $(document).ready(function() {
        $("select.links").change(function() {
             window.location = $(this).val();
        });
    });
</script>

使用此 html 片段:

<select class="links">
    <option disabled="disabled" selected="selected">—</option>
    <option value="http://www.google.com/">Google</option>
    <option value="http://www.yahoo.com/">Yahoo</option>
</select>

Maybe this is what you need: http://jgeewax.wordpress.com/2008/09/01/jquery-dropdown-fun/

EDIT - with props to the site mentioned above:

This is the final solution he gives:

<script type="text/javascript">
    $(document).ready(function() {
        $("select.links").change(function() {
             window.location = $(this).val();
        });
    });
</script>

With this html snippet:

<select class="links">
    <option disabled="disabled" selected="selected">—</option>
    <option value="http://www.google.com/">Google</option>
    <option value="http://www.yahoo.com/">Yahoo</option>
</select>
倒带 2024-08-11 01:37:16

我猜你想要选项中的值和 id?...试试这个:

<select id="listForms">
 <option value="">Select a Form</option>
 <option value="Personal Form" id="25">Personal Form</option>
 <option value="Employee Details Form" id="24">Employee Details Form</option>
 <option value="Contact Form" id="45">Contact Form</option>
</select>

<script type="text/javascript">
$(document).ready(function() {
 $("#listForms").change(function() {
  alert( "Selected Option: id = " + $(this).find('option:selected').attr("id") + ", " + $(this).val() );
 });
});
</script>

I'm guessing you want the vaule and id from the options?... try this:

<select id="listForms">
 <option value="">Select a Form</option>
 <option value="Personal Form" id="25">Personal Form</option>
 <option value="Employee Details Form" id="24">Employee Details Form</option>
 <option value="Contact Form" id="45">Contact Form</option>
</select>

<script type="text/javascript">
$(document).ready(function() {
 $("#listForms").change(function() {
  alert( "Selected Option: id = " + $(this).find('option:selected').attr("id") + ", " + $(this).val() );
 });
});
</script>
没有你我更好 2024-08-11 01:37:16

这是我的代码片段,我遇到的问题是在我选择一个选项并且页面 url 发生更改并向 URL 附加一个值后,我选择的值未设置为我选择的值,而是返回到“选择”默认值。

<select id="Phones">

    <option value="Select">select</option>
    <option value="Alcatel">Alcatel</option>
    <option value="BlackBerry">BlackBerry</option>
    <option value="HTC">HTC</option>
    <option value="Huawei">Huawei</option>
    <option value="LG">LG</option>
    <option value="Motorola">Motorola</option>
    <option value="Nokia">Nokia</option>
    <option value="Samsung">Samsung</option>
    <option value="Sony">Sony</option>
</select>


// Set the onchange event for the new dropdown
$("#Phones").change(function() { checkSelected(); });

// Run the onchange event to perform any default actions necessary
checkSelected();

function checkSelected() {
    // You could use the value of the dropdown in another SPServices request
    // here or just perform some other code



     var m = $('#Phones').find("option:selected").val();
     if (m !== 'Select')
     {

     var url = "/m.aspx?m=" + m;
     window.location = url;
     alert(m);



    }


}

This is my snippet code here The problem i have is after i select an option and the pages url changes and appends a value to the URL the value i selected isn't set to what i selected but goes back to "select" the default value.

<select id="Phones">

    <option value="Select">select</option>
    <option value="Alcatel">Alcatel</option>
    <option value="BlackBerry">BlackBerry</option>
    <option value="HTC">HTC</option>
    <option value="Huawei">Huawei</option>
    <option value="LG">LG</option>
    <option value="Motorola">Motorola</option>
    <option value="Nokia">Nokia</option>
    <option value="Samsung">Samsung</option>
    <option value="Sony">Sony</option>
</select>


// Set the onchange event for the new dropdown
$("#Phones").change(function() { checkSelected(); });

// Run the onchange event to perform any default actions necessary
checkSelected();

function checkSelected() {
    // You could use the value of the dropdown in another SPServices request
    // here or just perform some other code



     var m = $('#Phones').find("option:selected").val();
     if (m !== 'Select')
     {

     var url = "/m.aspx?m=" + m;
     window.location = url;
     alert(m);



    }


}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文