English 中文(简体)
需要结合阀和正文的指挥
原标题:Need a command that combines val() and text()
  • 时间:2011-10-12 21:16:10
  •  标签:
  • jquery

目前,我试图用 j子操纵两种不同类型的控制(标签和投入)的内容。 到目前为止,我正在通过使用两条指挥部这样做。

if(true)
{
   myControl.text( @types.Text );
   myControl.val( @types.Text );
}

是否有能够做到的指挥?

附带说明:我已尝试过html()和附录(),但我认为这些改动是案文()和阀()上的改动。

最佳回答

我在贾瓦Script文的公用事业中使用类似的东西。

$.fn.setValue = function(val) {
    return this.each(function() {
        switch (this.tagName.toLowerCase()) {
            case "input":
                switch ($(this).attr("type").toLowerCase()) {
                    case "hidden":
                    case "password":
                    case "text":
                        $(this).val(val); break;
                    case "radio":
                    case "checkbox":
                        this.checked = val; break;
                } break;
            case "span":
            case "button":
            case "label":
                $(this).html(val); break;
            case "select":
            case "textarea":
                $(this).val(val); break; 
        }
    });
};

然后,你可以利用你的新职能,几乎没有任何内容,而不必担心什么。

var myControl = $( #someSelector );
myControl.setValue( @Types.text );
问题回答

你可以写自己的话:

$.fn.tval = function (value) {
    if (this.is( :input:not(:checkbox):not(:radio) )) {
        this.val(value);
    }
    else {
        this.text(value);
    }
    return this;
}

没有将<编码>文本()与val(<>/code>合并的指令。 是否有这方面的书面习惯功能问题? 方式是:虽然text()html()几乎相同,但val(> append(<>) 是 VERY ——核对分类文件。

如果你也想支持阀和案文的左边:

$.fn.tval = function(value) {
    if (value) {
        if (this.is( :input:not(:checkbox):not(:radio) )) {
            this.val(value);
        } else {
            this.text(value);
        }
        return this;
    }else{
        if (this.is( :input:not(:checkbox):not(:radio) )) {
          return this.val();
        } else {
            return this.text();
        }
    }
}




相关问题
getGridParam is not a function

The HTML: <a href="javascript:void(0)" id="m1">Get Selected id s</a> The Function: jQuery("#m1").click( function() { var s; s = jQuery("#list4").getGridParam( selarrrow )...

selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

jQuery cycle page with links

I am using the cycle plugin with pager functionality like this : $j( #homebox ) .cycle({ fx: fade , speed: fast , timeout: 9000, pager: #home-thumbs , ...

jquery ui dialog opens only once

I have a button that opens a dialog when clicked. The dialog displays a div that was hidden After I close the dialog by clicking the X icon, the dialog can t be opened again.

jConfirm with this existing code

I need help to use jConfirm with this existing code (php & Jquery & jAlert). function logout() { if (confirm("Do you really want to logout?")) window.location.href = "logout.php"; } ...

Wrap text after particular symbol with jQuery

What I m trying to do, is wrap text into div inside ll tag. It wouldn t be a problem, but I need to wrap text that appears particularly after "-" (minus) including "minus" itself. This is my html: &...