【问题标题】:Adding Additional Text into Existing Placeholder Using Jquery使用 Jquery 将附加文本添加到现有占位符中
【发布时间】:2017-01-15 21:54:39
【问题描述】:

我有一个带有现有占位符的输入字段

<input type="text" name="property_price" id="property_price" placeholder="Monthly Rent" />

现在我有一个 ajax 函数,它根据表单上方的国家/地区选择框返回货币(卢比或美元)。我想将此 ajax 货币返回添加到现有占位符中,以便最终结果在占位符中看起来像这样

月租 (Rs.) 或 月租(美元)

这应该在占位符内。 “每月租金”的值也来自一个 jquery 函数,该函数显示“每月租金”或“售价”,具体取决于项目是出租还是出售。

ajax 函数货币返回结果如下

alert(msg);

初始占位符的值来自这个 Jquery 函数。

$('#property_availablefor').on('change', function() {

        if ( this.value == 'Rent')
        {
        $("#property_price").attr('placeholder', "Monthly Rent");

        }
        else if ( this.value == 'Sale')
        {
        $("#property_price").attr('placeholder', "Total Price");



        }

【问题讨论】:

    标签: jquery ajax placeholder


    【解决方案1】:

    像这样修改html:

    <input type="text" name="property_price" id="property_price" currency="USD" />
    

    全局设置

     var arr = {'Rent':"Monthly Rent",'Sale':"Total Price"};
    

    在 ajax 成功的数据属性中设置货币类型,如果您在rent/sale 元素之后更改国家/地区,则更新占位符

    $('#property_price').attr('currency',currency);
    if(  $('#property_availablefor').val().length > 0 ) {//check if the rent/sale element was  selected
         $("#property_price").attr('placeholder', $('#property_availablefor').val()+' '+ currency);
    }
    

    您更改的事件将如下所示:

    $('#property_availablefor').on('change', function() {
         var currency = $('#property_price').attr('currency');
            $("#property_price").attr('placeholder', arr[this.value]+' '+ currency);
    });
    

    【讨论】:

    • 这可以正常工作,但您会看到(我已经编辑了代码)我的初始占位符值也来自 jquery 函数。我应该在之前添加该代码。
    • 这似乎不起作用,我也试着摆弄它,也许我错过了什么。
    • 任何控制台错误,不工作是什么意思?
    • 好的,让我再次检查一下 jsfiddle 是否显示我想要的内容
    • 在我的代码中,如果我选择出租然后国家什么都没有发生,但是如果我选择国家然后出租它就可以工作......我的出租/销售字段在国家字段上方
    【解决方案2】:
    you can do the following:
    $(document).on('change', '<country select box>', function() {
    //your ajax call that returns the currency
    //get the currency value in a variable say 'currencyVal'
    //now assign the placeholder to the textbox.
    var intitialPlaceholderTxt = $("#property_price").attr('placeholder').substring(0, $("#property_price").attr('placeholder').indexOf('(') != -1 ? $("#property_price").attr('placeholder').indexOf('(') : $("#property_price").attr('placeholder').length);
    $("#property_price").attr('placeholder', intitialPlaceholderTxt + ' (' + currencyVal + ')');
    });
    

    【讨论】:

    • 用你的代码,有两件事,1.我需要在总价和货币之间留一个空格(我是php背景,所以请帮我解决这个小问题。2.如果你按国家再次选择它会继续添加越来越多的货币....
    猜你喜欢
    • 2014-11-24
    • 1970-01-01
    • 1970-01-01
    • 2013-01-18
    • 2012-01-03
    • 2018-11-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多