2

I am trying to build a jQuery selector. This involves concatenating string and variables. However, for some reason, the resulting selector comes up invalid. I have attempted a number of options such as this:

$('[name="[name='$radioName + (row_id) + ']"][value="$result"]').attr('checked',true);

$radioName example is: 'Nigeria[Osun][Okuku-;// the angle bracket is not closed as the row_id will be appended to it before the closing bracket is appended as shown above.

$result example is: '67

row_id example is: 5

How may I build a valid descriptor with these requirements?

Thanks

4

2 に答える 2

1

試す:

$('[name="' + $radioName + row_id + ']' + '"][value="' + $result + '"]').attr('checked',true);

連結式をまとめるときは、最初に適切な引用符を使用して静的テキストを記述します。何かのようなもの:

$('[name=""][value=""]').attr('checked',true);

'+ +'次に、変数を追加する各スポットに貼り付けます。そのようです:

$('[name="'+ +'"][value="'+ +'"]').attr('checked',true);

最後に、変数を追加します。

$('[name="'+ $radioName + row_id + ']' + '"][value="'+ $result +'"]').attr('checked',true);
于 2013-02-05T13:35:03.500 に答える
0

選択と比較を次のように分割する方がよいと思います。

$('[name][value]') // select all elements that have both name and value attributes
    .each(function() {
        var $this = $(this);

        if ($this.attr('name') == 'whatever' && $this.attr('value') == 'result') {
            $this.prop('checked', true);
        }
    });

またはそれらをフィルタリングします:

$('[name][value]') // select all elements that have both name and value attributes
    .filter(function() {
        var $this = $(this);

        return $this.attr('name') == 'whatever' && $this.attr('value') == 'result';
    })
    .prop('checked', true);
于 2013-02-05T13:38:42.363 に答える