doulun9534 2012-04-12 09:01
浏览 97
已采纳

HTML表单 - 如果未输入任何内容,则使用值填充输入

I'm creating a basic subscribe form for a system that requires both email address and last name (SugarCRM in case anyone is interested).

I need to populate the last name input value with something (anything, it doesn't matter) only if the visitor doesn't put their own last name in.

As the subscribe action is hosted (Mail Chimp) I don't have access to it to pre-validate, unless this can happen before it's passed on. I'm wading into deeper PHP waters than I'm comfortable with! Is this possible with jQuery?

So basically I'm getting around the required last name by filling the value with something.

  • 写回答

2条回答 默认 最新

  • dqc22586 2012-04-12 09:04
    关注

    Check the lastname on submit of the form, if it doesn't have value, put "foo" in it:

    $('#formId').submit(function() { // attach submit handler to the form.
        var $lastName = $('#lastName'); // get the last name element.
        if (!$lastName.val())   // if it doesn't have value.
            $lastName.val('foo'); // put foo as a value.
    });​
    

    You should replace formId and lastName to the actual ids of the form and last name element.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效