-2

我需要名字和姓氏的前两个字母,它将在像 Salman Shaikh 这样的文本框中输入,所以它会在我的页面中的某个位置作为 SASH 我需要这个。

4

5 回答 5

2

Here is live example with jQuery.

http://jsfiddle.net/rPTrK/

I'll paste the code in here also -

jQuery(document).ready(function() {
    jQuery("#submit").click(function() {
        var firstName = $("#name").val().substr(0, 2);
        var surname = $("#surname").val().substr(0, 2);
        $("body").append(firstName+surname);
    });        
});​

In case you don't know - jQuery is JS library. You can get it here . Download the latest jquery release and insert it in your html/php file between the tags, just like you normally insert js scripts.

Attention: You must insert my created code only after you have inserted the jQuery library, otherwise it won't work.

EDIT: Updated to your suggested version -

http://jsfiddle.net/pg8La/

Code -

jQuery(document).ready(function() {
    jQuery("#form1").change(function() {
        var firstName = $("#name").val().substr(0, 2);
        var surname = $("#surname").val().substr(0, 2);
        if(firstName != "" && surname != "") {
            $("body").append(firstName+surname);
        }
    });        
});​

EDIT 2: Final example per your request -

http://jsfiddle.net/K52fR/

And code here -

HTML

<form id="form1">
    <input type="text" name="name" id="name" placeholder="name" />
    <br />
    <input type="text" name="surname" id="surname" placeholder="surname" />
    <br />
    <h2 id="text">WSCPP<span id="nameSurname"></span><span id="time"></span><span id="randomNr"></span></h2> 
</form>​

jQuery/JS -

jQuery(document).ready(function() {
    jQuery("#form1").change(function() {
        var firstName = $("#name").val().substr(0, 2);
        var surname = $("#surname").val().substr(0, 2);
        if(firstName != "" && surname != "") {
            $("#nameSurname").text(firstName+surname);
        }
    });        
    var currentTime = new Date(); 
    var month = currentTime.getMonth() + 1;
    var day = currentTime.getDate(); var year = currentTime.getFullYear();
    $("#time").text(day + "" + month + "" +year);
    $("#randomNr").text(Math.floor(Math.random()*90000+9999));
});​
于 2012-08-23T08:16:07.167 回答
1

Check following for program

http://jsfiddle.net/YNV87/

于 2012-08-23T08:16:50.240 回答
1

How about this:

$_REQUEST['firstname'] = 'Salman ';
$_REQUEST['lastname'] = 'Shaikh';

$firstname = strtoupper(substr($_GET['firstname'], 0, 2));
$lastname = strtoupper(substr($_GET['lastname'], 0, 2));

echo $firstname.$lastname;
于 2012-08-23T08:18:01.293 回答
1

我假设您正在将数据发布到某个页面,这就是您php 的问题中有标签的原因。

$firstname = substr($_POST['firstname'],2);
$lastname = substr($_POST['lastname'],2);
于 2012-08-23T08:15:10.420 回答
0

Explode the string to an array.

$name_array=explode(' ',$name);
$first=substr(trim($name_array[0]),0,2);
$last=substr(trim($name_array[1]),0,2);
于 2012-08-23T08:16:34.687 回答