访问某些id的值,并使用它们将文本转换为链接

时间:2019-04-05 23:38:02

标签: javascript jquery html element

我想访问输入id的值并使用它们建立一个特定的链接,然后使用jquery使香蕉和苹果的值可点击,因为有多个div的“ panel-defaul”我想使所有这些可点击通过使用他们输入的id的值...

我已经尝试访问输入ID的值,但未成功

var id1 = $(“。input-group.input-group-lg”)。attr(“ market'0'.list'0'.made”)。value;

console.log(id1);

            <div class="panel-default">
                <div class="panel-body">
                    <div class="row-fluid">
                        <div class="col-md-8 col-lg-10">
                            <div class="row-fluid">
                                <div class="col-md-1">
                                </div>
                                <div class="col-md-11">
                                    <br>
                                    <table>
                                        <tbody>
                                            <tr align="left">
                                                <td><strong>1</strong></td>
                                                <td><i>Banana</i></td>
                                            </tr>
                                            <tr align="left">
                                                <td><strong>2</strong></td>
                                                <td><i>Apple</i></td>
                                            </tr>
                                        </tbody>
                                    </table>
                                </div>
                            </div>
                        </div>
                        <div class="col-md-4 col-lg-2">
                            <div class="input-group input-group-lg">

                                <input id="market'0'.list'0'.made" name="xxx1" value="Fruit1" type="hidden">

                                <input id="market'0'.list'1'.made" name="xxx2" value="Fruit2" type="hidden">
                            </div>

                        </div>
                    </div>
                </div>
            </div>

预期结果应如下:

香蕉是“可点击的”,重定向到链接“ www.xyz.com/Fruit1”

苹果是“可点击的”,重定向到链接“ www.xyz.com/Fruit2”

2 个答案:

答案 0 :(得分:0)

您的问题有点含糊,但是这是将值获取到DOM中的方法:

var $fruits = $(".input-group.input-group-lg").children();

var $newDiv = $('.links');
$fruits.each(function (index, el) {
  var $newLink = $('<a></a>');
  var link = 'http://www.example.com/' + el.value
  
  $newLink.text(el.value)
  $newLink.attr('href', link)
  $newDiv.append($newLink)
  $newDiv.append('<br />')
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="panel-default">
                <div class="panel-body">
                    <div class="row-fluid">
                        <div class="col-md-8 col-lg-10">
                            <div class="row-fluid">
                                <div class="col-md-1">
                                </div>
                                <div class="col-md-11">
                                    <br>
                                    <table>
                                        <tbody>
                                            <tr align="left">
                                                <td><strong>1</strong></td>
                                                <td><i>Banana</i></td>
                                            </tr>
                                            <tr align="left">
                                                <td><strong>2</strong></td>
                                                <td><i>Apple</i></td>
                                            </tr>
                                        </tbody>
                                    </table>
                                </div>
                            </div>
                        </div>
                        <div class="col-md-4 col-lg-2">
                            <div class="input-group input-group-lg">

                                <input id="market'0'.list'0'.made" name="xxx1" value="Fruit1" type="hidden">

                                <input id="market'0'.list'1'.made" name="xxx2" value="Fruit2" type="hidden">
                            </div>

                        </div>
                    </div>
                </div>
            </div>
            
            <div class="links">
            </div>

答案 1 :(得分:0)

请尝试这个。

var id1 = $(".input-group.input-group-lg input[name='xxx1']").val();
var id2 = $(".input-group.input-group-lg input[name='xxx2']").val();
console.log( id1 + ' | ' + id2 );