简体   繁体   English

用jQuery替换输入值.val()

[英]Replace input value .val() with jQuery

So basically here is my jsFiddle - http://jsfiddle.net/CmNFu/ . 所以基本上这里是我的jsFiddle- http://jsfiddle.net/CmNFu/

And code also here - 还有代码在这里-

HTML - HTML-

<b style="float: left; margin-right: 10px;">category 1</b><input type="checkbox" value="category1" style="float: left;" class="portfolio-category" /><br />
<b style="float: left; margin-right: 10px;">category 2</b><input type="checkbox" value="category2" style="float: left;" class="portfolio-category" /><br />
<br />
<br />
<input type="text" name="categories" id="portfolio-categories" />​

jQuery - jQuery-

jQuery(document).ready(function() {
    jQuery(".portfolio-category").click(function() {
        if(jQuery(this).is(":checked")) {
            jQuery("#portfolio-categories").val(jQuery("#portfolio-categories").val()+" "+jQuery(this).val());
        }
        else {
            var portfolioCategories = jQuery("#portfolio-categories").val();    
            alert("before + "+portfolioCategories);
            var currentElement = jQuery(this).val()+" ";
            alert(currentElement);
            portfolioCategories = portfolioCategories.replace(currentElement, "");
            alert(portfolioCategories);
        }
    });
});

​Well basically what I would like to achieve is, when user checks the checkbox, the value automatically adds inside input field (Done, it's working, whooray!), but the problem is when it unchecks the checkbox, the value should be removed from input box (the problem starts here), it doesn't remove anything. 基本上我想实现的是,当用户选中复选框时,该值会自动添加到输入字段中(完成,它正在工作,哇!),但是问题是当它取消选中该复选框时,应该从中删除该值输入框(问题从此处开始),它不会删除任何内容。 You can see I tried assigning val() function to variables, but also without success. 您可以看到我尝试将val()函数分配给变量,但是也没有成功。 Check my example on jsFiddle to see it live. 查看我在jsFiddle上的示例以实时观看。

Any suggestions? 有什么建议么? I guess replace() is not working for val(), is it? 我猜replace()对val()不起作用,对吗?

So, is there any other suggestions? 那么,还有其他建议吗?

I'd do this: 我会这样做:

jQuery(document).ready(function() {
    jQuery(".portfolio-category").on('change', function() {
        var string = "";
        $('input[type="checkbox"]').each(function() {
            var space = string.length>0?' ':'';
            string += this.checked?space+this.value:'';
        });
        $("#portfolio-categories").val(string);
    });
});

FIDDLE 小提琴

You have quite the issue with spaces in that input box. 您在该输入框中存在空格问题。 but we'll get to that in a moment. 但我们稍后会解决。

first, this will kind of work (if it weren't for the spaces problem): 首先,这将是一种工作(如果不是因为空间问题):

add this line before the last alert: 在最后一个警报之前添加此行:

 jQuery("#portfolio-categories").val(portfolioCategories);

this will work, but not always, as the last element you append doesn't have a space after it. 这将起作用,但并非总是如此,因为您添加的最后一个元素后面没有空格。

but if you change the 4th line to this: 但是,如果将第四行更改为:

jQuery("#portfolio-categories").val(jQuery("#portfolio-categories").val()+jQuery(this).val()+" ");

it will work, as it adds the space after each element, instead of before. 它会起作用,因为它会在每个元素之后而不是之前添加空格。

http://jsfiddle.net/CmNFu/5/ http://jsfiddle.net/CmNFu/5/

your issue was that you changed the values in the variable: portfolioCategories , but you haven't updated the input itself. 您的问题是您更改了以下变量的值: portfolioCategories ,但尚未更新输入本身。 (notice, changing the value of a string, doesn't change the value of the input it originally came from) (注意,更改字符串的值不会更改其原始来源的输入的值)

What you need is to insert back the string portfolioCategories into the input. 您需要将字符串PortfolioCategories插入回输入中。 Also the spaces are creating a lot of problems. 同样,空间也产生了很多问题。 You could use $.trim(str) to remove any leading and trailing spaces from a string. 您可以使用$ .trim(str)从字符串中删除任何前导和尾随空格。 Have updated your fiddle with a solution that works. 已使用有效的解决方案更新了您的小提琴。

http://jsfiddle.net/CmNFu/11/ http://jsfiddle.net/CmNFu/11/

Hope this helps. 希望这可以帮助。

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

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