jQuery:如何访问此输入值 [英] jquery: how do I access this input value

查看:102
本文介绍了jQuery:如何访问此输入值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

演示: http://jsfiddle.net/mjmitche/sTzCS/39/

我有这个表格,里面有一个无序列表.我已将jquery的sortable()函数附加到列表中,因此用户可以上下移动表单元素以指示团队将完成的顺序,而下面的jquery确定用户将其放置的顺序.为每个列表元素创建一个输入字段,以便用户可以输入每个团队将获得的积分数.

I have this form with an unordered list inside of it. I have attached jquery's sortable() function to the list, so users can move the form elements up or down to indicate which order the teams will finish in, and the jquery below determines what order the user has placed them in. However, I also create an input field for each of the list elements, so that users can enter in the number of points each team will get.

例如,用户可以将Winnipeg移动到列表的顶部,然后在输入字段中输入值95.因此,温尼伯将是第一名,并获得95分(根据用户对球队排名的预测).

For example, a user might move Winnipeg to the top of the list, and enter the value 95 in the input field. Therefore, Winnipeg would be first and finish with 95 points (according to the user's prediction of team standings).

我的问题是,如何以与团队相关联的方式访问每个表单元素的输入值.例如,如果将Winnipeg放在第一位,则根据下面的代码,可以在数组的result[0]上对其进行访问.但是,我不认为输入值是数组的一部分,所以我不知道如何访问它们.

My question is, how do I access the input value for each of the form elements in such a way that they are associated with the team. For example, if Winnipeg is placed first, it will, according to the code below, be accessible at result[0] of the array. However, I don't think the input values are part of the array, so I don't know how to access them.

我可以使用attribute.first访问第一小组,但是即使输入值看起来像子元素,也无法获得attribute.first.child的分数.

I was able to access the first team using attribute.first, but couldn't get the points doing attribute.first.child even though the input value seems to look like a child element.

我最终希望能够将每个团队及其位置编号(由排序位置确定)和点(由用户手动输入确定)保存到数据库中

I ultimately want to be able to save each team to the database with their position number (determined by sort position) and points (determined by user manual input)

html

<form<form id="new_entry">
<ul id="sortlist">

        <li id="Vancouver">Canucks<input type="text" name="name" id="vanpoints"></li>
        <li id="Toronto">Toronto<input type="text" name="name" id="torontopoints"></li>
        <li id="Montreal">Montreal<input type="text" name="name" id="montrealpoints"></li>
        <li id="Ottawa">Ottawa<input type="text" name="name" id="ottawapoints"></li>
        <li id="Calgary">Calgary<input type="text" name="name" id="calgarypoints"></li>
        <li id="Edmonton">Edmonton<input type="text" name="name" id="edmontonpoints"></li>
        <li id="Winnipeg">Winnipeg<input type="text" name="name" id="winnipegpoints"></li>
</ul>
<input type="submit" value="Add">
</form>​

代码

$("#sortlist").sortable({
    stop: function(event, ui) {
        result = $("#sortlist").sortable("toArray");
        var attributes;
        attributes = {
            first: result[0],
            second: result[1],
            third: result[2],
            fourth: result[3],
            fifth: result[4],
            six: result[5],
            seventh: result[6]

        };

        alert(attributes.first.child);



    }
});​

推荐答案

尝试将值另存为哈希值:

Try saving the values as a hash instead:

...
   first: { team: result[0].id, value: result[0].children()[0].val() }
...

然后以"attribute.first.team和attributes.first.value"的身份访问团队和值.

And then access the team and value as attributes.first.team and attributes.first.value.

这篇关于jQuery:如何访问此输入值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆