简体   繁体   中英

Form Hidden Field Substitution

I'm building a form using Expression Engine's Safecracker module.

One of the the required fields is the Title, which becomes the title of the EE channel entry.

What I'd like to do is set the Title field to be the combined first name and last name fields.

I started with this:

<form method="POST" action="#">

<input id="student_first_name" type="text" size="30" name="student_first_name"> 
<br>
<input id="student_last_name" type="text" size="30" name="student_last_name"> 
<br><br>
<input type="text" name="title" value=""/> 

</form>

And then added this:

$(function() {
    $('#student_first_name').keyup(function() {
        var snamef = $(this);
    });
    $('#student_last_name').keyup(function() {
        var snamel = $(this);
    });
    $("input[name='title']").val(snamel + " " + snamef);
    return false;
});​

​I can't get it to work, though: http://jsfiddle.net/tylonius/CY5zJ/4/

Am I missing a step (or just totally doing it wrong?)?

Also, am I possibly working too hard and Safecracker already has this function built in; similar to its live UrlTitle(); function?

Any help is appreciated.

Thanks,

ty

If I understood right your question try this:

First set an id on your desired input element, like this :

<input type="text" name="title" id="student_title" value=""/>

And then :

$(function() {  
    changeFunction = function() {
        $('#student_title').val($('#student_first_name').val() + ' ' + $("#student_last_name").val());
    }

    $('#student_first_name').keyup(changeFunction)
    $('#student_last_name').keyup(changeFunction);   
});

更好的是,避免完全使用javascript,而只需使用SafeCracker的dynamic_title参数

dynamic_title="[student_first_name] [student_last_name]"

user1236048 has the best solution for you but below is a working version of your code

$(function() {

    var snamef;
    var snamel;

    $('#student_first_name').keyup(function() {
         snamef = $(this).val();
         $("input[name='title']").val(snamel + " " + snamef);
    });
    $('#student_last_name').keyup(function() {
         snamel = $(this).val();
         $("input[name='title']").val(snamel + " " + snamef);
    });
    return false;
});

and here is the working jsfiddle

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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