简体   繁体   English

jQuery函数到引导模态中文本的结果

[英]Result from jquery function to text in bootstrap modal

So my problem is that i have a JQuery function and i want to put the result in a text label in a Bootstrap modal. 所以我的问题是我有一个JQuery函数,我想将结果放入Bootstrap模式的文本标签中。 The real problem is that i cant make a link between the Jquery and Bootstrap. 真正的问题是我无法在Jquery和Bootstrap之间建立链接。 ex. 例如 : When the user click on a word, the Bootstrap modal open with the word which was clicked and after the user will change it. :当用户单击一个单词时,Bootstrap模态会打开并带有被单击的单词,用户将对其进行更改。 It will be great if you can help me. 如果您能帮助我,那就太好了。

<div class="container">
    <div class="well well-large">
        <p class="clickable" >
         Change this text and insert some words. 
        </p>
        <div id="form-content" class="modal hide fade in" style="display: none;">
            <div class="modal-header">
                <a class="close" data-dismiss="modal">×</a>
                <h3>Change the word</h3>
            </div>
            <div class="modal-body">
                <form class="contact" name="contact">
                    <label class="label" for="name">Your Text</label><br>
                    <input type="text" name="name" class="input-xlarge"><br>
                </form>
            </div>
            <div class="modal-footer">
                <input class="btn btn-success" type="submit" value="Submit" id="submit">
                <a href="#" class="btn" data-dismiss="modal">Back</a>
            </div>
        </div>
        <div id="thanks"><p><a data-toggle="modal" href="#form-content" class="btn btn-primary btn-large">Modal powers, activate!</a></p></div>
    </div>
</div>

and the Jquery function is 而Jquery函数是

<script>

        $(".clickable").click(function(e){
             s = window.getSelection();
             var range = s.getRangeAt(0);
             var node = s.anchorNode;
             while(range.toString().indexOf(' ') != 0) {                 
                range.setStart(node,(range.startOffset -1));
             }
             range.setStart(node, range.startOffset +1);
             do{
               range.setEnd(node,range.endOffset +1);

            }while(range.toString().indexOf(' ') == -1 && range.toString().trim() != '');
            var str = range.toString().trim();

        $('#form-content').modal('show');
//      return str;
//      alert(str);

       });


    </script>

After you get your str : 获得str

var str = range.toString().trim();

you can set it as the value of the input on your modal like so: 您可以将其设置为模态输入的值,如下所示:

$('.modal-body .contact input').val(str);

( Or you could just assign this input an ID and do $('#inputid').val(str); ) (或者您可以为该输入分配一个ID并执行$('#inputid').val(str);

Full example: JSFiddle 完整示例: JSFiddle

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM